Even though you have seen numerous examples of these techniques in action, you may still be wondering when to make use of reflection, dynamic loading, late binding, and custom attributes in your programs? To be sure, these topics can seem a bit on the academic side of programming (which may or may not be a bad thing, depending on your point of view). To help map these topics to a real-world situation, you need a solid example. Assume for the moment that you are on a programming team that is building an application with the following requirement:
The product must be extendable by the use of additional third-party tools.
What exactly is meant by extendable? Well, consider the Visual Studio 2010 IDE. When this application was developed, various “hooks” were inserted into the code base to allow other software vendors to “snap” (or plug-in) custom modules into the IDE. Obviously, the Visual Studio 2010 development team had no way to set references to external .NET assemblies it had not developed yet (thus, no early binding), so how exactly would an application provide the required hooks? Here is one possible way to solve this problem:
Simply put, if the extendable application has been preprogrammed to query for specific interfaces, it is able to determine at runtime whether the type can be activated. Once this verification test has been passed, the type in question may support additional interfaces that provide a polymorphic fabric to their functionality. This is the exact approach taken by the Visual Studio 2010 team, and despite what you may be thinking, is not at all difficult!